package com.sunnada.edu.system.quartz.timers;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;

import com.sunnada.edu.business.attendance.service.AttendanceService;
import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.dao.PubDao;

/**
 * 
 *@ClassName: UpdateAttendanceApplyState
 *@Description: 系统默认修改考勤异常申请的状态
 * @author: lbn
 *@Date: 2017-6-20 上午10:05:28
 */
@Service
public class UpdateAttendanceApplyState implements Job {
	Logger log  =  Logger.getLogger(UpdateAttendanceApplyState.class );
	
	@Resource
	AttendanceService attendanceService;
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		log.info("------------[当前时间："+context.getFireTime()+"[每天考勤统计]----------------");
		updateState();
	}
	
	/*
	 * 修改异常考勤申请的审核状态和考勤统计的状态
	 */
	private void updateState() {
		PubDao dao = AppContext.getInstance().getPubDao();
		
		//修改考勤统计的状态
		String sql = "SELECT w.JOBNO, t.BEGIN_DATE, t.END_DATE FROM "
                   + "(SELECT t.* FROM t_attendance_apply_worker t WHERE t.APPLY_ID IN (SELECT t.SUBMIT_ID FROM t_process_result t WHERE t.AUDIT_RESULT = '3' AND t.PROCESS_ID = "
                   + "(SELECT t.REC_ID FROM t_process t WHERE t.type = 'attendanceApply')) AND t.REJECT_STATE = '1') t "
                   + "LEFT JOIN t_worker w ON t.WORKER_ID = w.REC_ID";
		List<?> list = dao.selectList(sql);
		attendanceService.abnormalAttendance(list);
		
		//修改考勤异常审核的状态
		String updateSql = "UPDATE t_process_staff t SET t.AUDIT_STATE = '1', t.task_type = '2', t.VIEW_FLAG = '1', t.AUDIT_DESC = '系统默认审核', t.REC_UPDATETIME = NOW(), t.REC_UPDATEPERSON = '系统默认' WHERE t.PROCESS_ID = "
                         + "(SELECT s.REC_ID FROM t_process s WHERE s.type = 'attendanceApply')";
		dao.update(updateSql);
		
		updateSql = "UPDATE t_process_result t SET t.AUDIT_RESULT = '1', t.REC_UPDATETIME = NOW(), t.REC_UPDATEPERSON = '系统默认' WHERE t.PROCESS_ID = "
                  + "(SELECT s.REC_ID FROM t_process s WHERE s.type = 'attendanceApply')";
		dao.update(updateSql);
	}

	
	

}
